home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 22 / PC Actual CD 22.iso / progs / Netobj / CDK / NetObjectsFusionCDK5_97.exe / _SETUP.1 / FormHandlerComp.java < prev    next >
Encoding:
Java Source  |  1997-05-14  |  11.1 KB  |  452 lines

  1. import ComponentApp;
  2.  
  3. public class FormHandlerComp extends ComponentApp
  4. {
  5.     DAssetManager cam;
  6.     DDrawFormButton cdp; 
  7.     int          ImageID;
  8.     int          perlScriptID;
  9.     String SubmitName;
  10.     int SuccessURL;
  11.     int ErrorURL;
  12.     String outputFile; //should it be the whole path 
  13.     
  14.     String UnixWindows;
  15.     String PerlPath;
  16.     String storageDir;
  17.     String codeBase;
  18.  
  19.     static final int MAXASSET=50;
  20.     boolean published = false;
  21.     boolean reqField[];
  22.     int reqFieldCount = 0;
  23.                 
  24.     int assetControl[];
  25.     int assetCount = 0;
  26.     int tAssetCount = 0;
  27.     int actionCGIAsset = 0;
  28.     
  29.     int min(int i, int j){
  30.         if (i < j)
  31.             return i;
  32.         else
  33.             return j;
  34.     }
  35.     int max(int i, int j){
  36.         if (i > j)
  37.             return i;
  38.         else
  39.             return j;
  40.     }
  41.     
  42.     String cleanPath(String path){
  43.  
  44.       String temp = new String(path);
  45.       String temp2 = new String(path);
  46.       String temp3 = new String(path);
  47.       
  48.       int len = path.length();
  49.      //sub to home
  50.       if(temp.substring(0,3).compareTo("../") == 0){
  51.          path = "../" + path;
  52.       }
  53.       //home to sub, sub to sub
  54.       else if(temp2.substring(0,7).compareTo("../html") == 0){
  55.          path = "../" + path;
  56.       }
  57.       //home to home
  58.       else if(temp3.substring(0,2).compareTo("./") == 0){
  59.          path = "../." + path;
  60.       }
  61.  
  62.       return path;
  63.           
  64.       
  65.  
  66.     }
  67.  
  68.  
  69.     String getFieldName(int i){
  70.         int temp = i + 1;
  71.         return "Required field "+ temp;
  72.     }
  73.  
  74.  
  75.     String getHiddenInsert(int context){
  76.         String hold;
  77.     //    if (cam.GetAssetLocation(SuccessURL,context,IFileNameStyle.DosName).substring(0,4))
  78.         
  79.         hold = "<INPUT TYPE=\"HIDDEN\" NAME=\"success\" VALUE=\"" + cleanPath(cam.GetAssetLocation(SuccessURL,context,IFileNameStyle.DosName))+"\">\n";
  80.         hold = hold + "<INPUT TYPE=\"HIDDEN\" NAME=\"error\" VALUE=\"" + cleanPath(cam.GetAssetLocation(ErrorURL,context,IFileNameStyle.DosName))+"\">\n";
  81.  
  82.  
  83.         hold = hold + "<INPUT TYPE=\"HIDDEN\" NAME=\"fields\" VALUE=\"" + reqFieldCount + "\">\n";
  84.         hold = hold + "<INPUT TYPE=\"HIDDEN\" NAME=\"filename\" VALUE=\"" + outputFile + "\">\n";
  85.         int tempK;
  86.         for (int k=0; k < reqFieldCount; k++){
  87.             if (reqField[k] == true){
  88.                 tempK = k + 1;
  89.                 hold = hold + "<INPUT TYPE=\"HIDDEN\" NAME=\"" + tempK + "a\" VALUE=\"TRUE\">\n";
  90.             }
  91.         }
  92.         return hold;
  93.         
  94.     }
  95.  
  96.     int countElement()
  97.     {
  98.         IDLayout cidLayout = cdp.getLayout();
  99.         int count = 1;
  100.         boolean tempField[];
  101.         int tempCounter = 0;
  102.  
  103.         while (true) {
  104.             String name = new String() + count;
  105.             if ( cidLayout.IsElementName(name) ) {
  106.                 ++count;
  107.                 continue;
  108.             }
  109.             else {
  110.                 count--;
  111.                 break;
  112.             }
  113.         }
  114.  
  115.         if (reqField == null){
  116.             reqField = new boolean [count];
  117.             for (tempCounter = 0; tempCounter < count; tempCounter++){
  118.                 reqField[tempCounter] = false;
  119.             }
  120.             reqFieldCount = count;
  121.             return count;    
  122.         }
  123.  
  124.         int minCount = min(reqFieldCount,count);
  125.  
  126.         tempField = reqField;
  127.         reqField = new boolean [count];
  128.         //take the min of two.
  129.  
  130.         for (tempCounter = 0; tempCounter < minCount; tempCounter++){
  131.             reqField[tempCounter] = tempField[tempCounter];
  132.         }
  133.         if (reqFieldCount != max(reqFieldCount, count)){
  134.             //make sure new array is larger, so false setting is justify.
  135.             //if old array, then skip this seciton.
  136.             for (; tempCounter < max(reqFieldCount, count); tempCounter++){
  137.                 reqField[tempCounter] = false;
  138.             }
  139.         }
  140.         //min to max.
  141.         //initialize whatever have not been copy... which is false.
  142.  
  143.         
  144.  
  145.         reqFieldCount = count;
  146.         return count;
  147.        }
  148.  
  149.     
  150.     public String onInstall(DAssetManager asm, String codeBaseOut) 
  151.     {
  152.     //    assetControl = new int [MAXASSET];
  153.         cam = asm;
  154.         codeBase = codeBaseOut;
  155.         return "Form Handler";
  156.     }
  157.                     
  158.     public void onInspect(CStringArray Names, CStringArray Types)
  159.     { 
  160.         Names.Set("Button Name");
  161.         Types.Set(typeString);
  162.         Names.Set("Success URL");
  163.         Types.Set(typeLink);
  164.         Names.Set("Error URL");
  165.         Types.Set(typeLink);
  166.         Names.Set("Output File");
  167.         Types.Set(typeString);
  168.         Names.Set("Publish to");
  169.         Types.Set("Set(Windows|Unix|Mac)");
  170.         Names.Set("Perl Path for Unix");
  171.         Types.Set(typeString);
  172. //        Names.Set("Storage Directory");
  173. //        Types.Set(typeString);
  174.         int tempCounter=countElement();
  175.         for(int marker=0; marker < tempCounter; marker++){
  176.             Names.Set(getFieldName(marker));
  177.             Types.Set("Set(True|False)");
  178.         }
  179.         
  180.     }
  181.     
  182.     
  183.     public String PropertyListener(String Event,String Value,int Get, int propIndex, IDInspector insp) 
  184.     {
  185.         if (Get == 1)
  186.         {
  187.             if (Event.compareTo("Button Name") == 0)
  188.             {
  189.                 return SubmitName;
  190.             }
  191.             else if (Event.compareTo("Success URL") == 0)
  192.             {
  193.                 return Integer.toString(SuccessURL);
  194.             }
  195.             else if (Event.compareTo("Error URL") == 0)
  196.             {
  197.                 return Integer.toString(ErrorURL);
  198.             }
  199.             else if (Event.compareTo("Output File") == 0)
  200.             {
  201.                 return outputFile;
  202.             }
  203.             else if (Event.compareTo("Publish to") == 0)
  204.             {
  205.                 if(UnixWindows.compareTo("Windows") == 0)
  206.                     return "0";
  207.                 else if (UnixWindows.compareTo("Unix") == 0)
  208.                     return "1";
  209.                 else if (UnixWindows.compareTo("Mac") == 0)
  210.                     return "2";
  211.  
  212.             }
  213.             else if (Event.compareTo("Perl Path for Unix") == 0)
  214.             {
  215.                 return PerlPath;
  216.             }
  217. //            else if (Event.compareTo("Storage Directory") == 0)
  218. //            {
  219. //                return storageDir;
  220. //            }
  221.         
  222.             for(int marker=0; marker < countElement(); marker++){
  223. //                marker++;
  224.                 if (Event.compareTo(getFieldName(marker)) == 0)
  225.                 {
  226.                     if( reqField[marker]==false ){
  227.                         return "1";
  228.                     }
  229.                     else {
  230.                         return "0";
  231.                     }
  232.                 }
  233.             }
  234.         }
  235.         else
  236.         {
  237.             if (Event.compareTo("Button Name") == 0)
  238.             {
  239.                 SubmitName = Value;
  240.                 cdp.setText(SubmitName);
  241.             }
  242.             else if (Event.compareTo("Success URL") == 0)
  243.             {     
  244.                 SuccessURL = Integer.parseInt(Value,10);                
  245.             }
  246.             else if (Event.compareTo("Error URL") == 0)
  247.             {
  248.                 ErrorURL = Integer.parseInt(Value,10);                
  249.             }
  250.             else if (Event.compareTo("Output File") == 0)
  251.             {
  252.                 outputFile = Value;
  253.             }
  254.             else if (Event.compareTo("Publish to") == 0)
  255.             {
  256. //                if(Value.compareTo("0")==0)
  257. //                    UnixWindows=true;
  258.                 //to unix
  259. //                else
  260. //                    UnixWindows=false;
  261.                 //to window
  262.                 if (Value.compareTo("0") == 0)
  263.                     UnixWindows = "Windows";
  264.                 else if (Value.compareTo("1") == 0)
  265.                     UnixWindows = "Unix";
  266.                 else
  267.                     UnixWindows = "Mac";
  268.  
  269.                 
  270.             }
  271.             else if (Event.compareTo("Perl Path for Unix") == 0)
  272.             {
  273.                 PerlPath = Value;
  274.             }
  275. //            else if (Event.compareTo("Storage Directory(Unix)") == 0)
  276. //            {
  277. //                storageDir = Value;
  278. //            }
  279.             
  280.             for(int marker=0; marker < countElement(); marker++){
  281.  
  282.                 if (Event.compareTo(getFieldName(marker)) == 0)
  283.                 {
  284.                     if(Value.compareTo("0")==0)
  285.                     {
  286.                         reqField[marker]=true; 
  287.                      }
  288.                     else {
  289.                           reqField[marker]=false; 
  290.                     }
  291.                 }
  292.             }
  293.             
  294.         }
  295.         return "";
  296.     }
  297.     
  298.     
  299.     
  300.     public void onCopy()
  301.     {
  302.     
  303.         SubmitName = new String(SubmitName);
  304.         outputFile  = new String(outputFile);  //should it be the whole path 
  305.         PerlPath  = new String(PerlPath); 
  306.         storageDir  = new String(storageDir); 
  307.         codeBase = new String(codeBase);
  308.  
  309.         boolean [] temp = new boolean[reqFieldCount];
  310.  
  311.         for (int i=0; i < reqFieldCount; i++)
  312.             temp[i] = reqField[i];
  313.  
  314.         reqField = temp;
  315.  
  316.  
  317.         for(tAssetCount = 0; tAssetCount < assetCount; tAssetCount++){
  318.             cam.CopyAsset(assetControl[tAssetCount]);
  319.         }
  320.             
  321.  
  322.         
  323.  
  324.  
  325.  
  326.     }
  327.     
  328.     public void onDrop(IDLayout layout, IDRect r, int fDrop) 
  329.     {
  330.         
  331.         //
  332.         // we only process drop for now
  333.         //
  334.         if (fDrop != ActivateState.Drop)
  335.             return;
  336.  
  337.         assetControl = new int [MAXASSET];
  338.         for (int tCount = 0; tCount < MAXASSET; tCount++){
  339.             assetControl[tCount] = 0;
  340.         }
  341.  
  342.  
  343.         DFile theScript = new DFile();
  344. //        String FusionPath = cdp.getCodeBase();
  345. //        String FusionPath = theScript.getExeLocation() + "NetObjects System\\Components\\FormHandlerComp\\";
  346.         String FusionPath = codeBase;
  347.  
  348.         //need to get # of form elements from David
  349.         
  350.         SubmitName="Submit";
  351.         outputFile="webinfo";
  352.         UnixWindows="Windows";
  353.         PerlPath="/usr/local/bin/perl";
  354.         storageDir="../webdata/";
  355.  
  356.         cdp = new DDrawFormButton();
  357.         cdp.setText(SubmitName);
  358.         layout.AddObject(cdp);
  359.     
  360.         cdp.SetPositionRect(r.getLeft(), r.getTop(), r.getRight(), r.getBottom());
  361.     }                                 
  362.     
  363.     
  364.     protected void finalize(){
  365.         
  366.         if (published == true){
  367.           for(tAssetCount = 0; tAssetCount < assetCount; tAssetCount++){
  368.                  cam.RemoveAsset(assetControl[tAssetCount]);
  369.           }
  370.  
  371.         }
  372.  
  373.         
  374.     }
  375.  
  376.  
  377.     public void onPublish(DAssetManager asm, int context)
  378.     {
  379.         IDLayout cidLayout = cdp.getLayout();
  380.         int count = countElement();
  381.  
  382.         DFile file = new DFile();
  383.         DFile fileFrom = new DFile();
  384.  
  385.         DFile theScript = new DFile();
  386.         String FusionPath = codeBase;
  387.         String pathChar = theScript.GetPathChar();
  388.  
  389.         DMessageBox mBox = new DMessageBox();
  390.         
  391.         if (published == true){
  392.             for(tAssetCount = 0; tAssetCount < assetCount; tAssetCount++){
  393.                 cam.RemoveAsset(assetControl[tAssetCount]);
  394.             }
  395.         }
  396.         assetCount = 0;
  397.         published = true;
  398.  
  399.  
  400.         cdp.setHTMLBefore(this.getHiddenInsert(context));
  401.         
  402.         if(UnixWindows.compareTo("Windows") == 0){
  403.              //NT version
  404.                        
  405.             actionCGIAsset = cam.AddAsset(FusionPath+"Forms" + pathChar + "cgi-bin-NT" + pathChar + "Forms-Handler.cgi", IAssetType.CGI,"assets" + pathChar + "cgi-bin");
  406.             assetControl[assetCount] = actionCGIAsset;
  407.             cam.SetPublishAssetMode(assetControl[assetCount++], 0705);
  408.             assetControl[assetCount] = cam.AddAsset(FusionPath+"Forms" + pathChar + "cgi-bin-NT" + pathChar + "Buf.dll", IAssetType.CGI,"assets" + pathChar + "cgi-bin");
  409.             cam.SetPublishAssetMode(assetControl[assetCount++], 0705);
  410.         }
  411.         if(UnixWindows.compareTo("Mac") == 0){
  412.              //Mac version
  413.                        
  414.             actionCGIAsset = cam.AddAsset(FusionPath+"Forms" + pathChar + "cgi-bin-mac" + pathChar + "Forms-Handler.cgi", IAssetType.CGI,"assets" + pathChar + "cgi-bin");
  415.             assetControl[assetCount] = actionCGIAsset;
  416.             cam.SetPublishAssetMode(assetControl[assetCount++], 0705);
  417.             assetControl[assetCount] = cam.AddAsset("assets" + pathChar + "webdata" + pathChar + "",IAssetType.SubDir, "");
  418.             cam.SetPublishAssetMode(assetControl[assetCount++], 0703);
  419.         }
  420.         else {
  421.             //Unix version
  422.             
  423.             assetControl[assetCount] = cam.AddAsset("assets" + pathChar + "webdata" + pathChar + "",IAssetType.SubDir, "");
  424.             cam.SetPublishAssetMode(assetControl[assetCount++], 0703);
  425.  
  426.  
  427.             fileFrom.Open(FusionPath+"Forms" + pathChar + "cgi-bin-Unix" + pathChar + "Forms-Handler.cgi",FileOpenMode.modeRead);
  428.             file.Open(FusionPath+"Forms" + pathChar + "cgi-bin" + pathChar + "Forms-Handler.cgi",FileOpenMode.modeWrite|FileOpenMode.modeCreate);
  429.             file.WriteString("#!"+PerlPath+"\n", -1);
  430.             file.WriteString("$STORAGEDIR=\""+storageDir+"\";", -1);
  431.             file.WriteString(fileFrom.ReadString(10000),-1);
  432.             file.Close();
  433.             fileFrom.Close();
  434.             actionCGIAsset = cam.AddAsset(FusionPath+"Forms" + pathChar + "cgi-bin" + pathChar + "Forms-Handler.cgi", IAssetType.CGI,"assets" + pathChar + "cgi-bin");
  435.             assetControl[assetCount] = actionCGIAsset;
  436.             cam.SetPublishAssetMode(assetControl[assetCount++], 0705);
  437.  
  438.  
  439.         }
  440.  
  441.  
  442.  
  443.         String cgiPath = cam.GetAssetRelativeLocation(actionCGIAsset, context, IFileNameStyle.HTMLName);
  444.         
  445.         
  446.  
  447.         cidLayout.setFormAction(cgiPath);
  448.  
  449.     }
  450.     
  451.  
  452. }